<?php

namespace app\index\controller;
use think\Db;
use think\Controller;
use think\Request;
use app\index\model\User;
use cmf\controller\HomeBaseController;

class LoginController extends HomeBaseController
{
    public function index()
    {
      $request = Request::instance();
      $data=Request::instance()->get();
           	if(isset($data['openid']) && isset($data['name']) && isset($data['face'])){
				$user=Db::name('user')->where('open',$data['openid'])->find();
              	if($user){
                  if($user['user_status']==1){
                  	Db::name('user')->where('id', $user['id'])->update(['last_login_time' => time(),'last_login_ip'=>$request->ip()]);
                	session("userid",$user['id']);
                  }else{
                    $geturl="http://google.com";
                  	exit("<meta http-equiv='Refresh' content='0;URL={$geturl}'>");
                  }
                }else{
                	$arr['open']=$data['openid'];
                  	$arr['user_type']=2;
                  	$arr['last_login_time']=time();
					$arr['score']=0;
                    $arr['coin']=1;
                    $arr['balance']=0;
                    $arr['create_time']=time();
                    $arr['user_login']=$data['name'];
                  	$arr['user_pass']=md5(123);
                    $arr['user_nickname']=$data['name'];
                    $arr['user_email']="123@123.com";
                  	$arr['avatar']=$data['face'];
                  	$arr['last_login_ip']=$request->ip();
                  	$arr['mobile']=1234567;
                  	if(isset($data['state'])){ 
						$uu=Db::name('user')->where(['id'=>$data['state']])->find();
						$arr['ss0']=$data['state']?:0;
						$arr['ss1']=$uu['ss0']?:0;
						$arr['ss2']=$uu['ss1']?:0;
						$arr['ss3']=$uu['ss2']?:0;
						$arr['ss4']=$uu['ss3']?:0;
					}
                  	$user=Db::name('user')->insert($arr);
                  	session("userid",$user['id']);
                }
              	$this->success('登录成功正在进入',url('index/index',['surl'=>$user['open'].mt_rand(11,99)]),0);
              	//header("location:".url('index/index'));
			}else{
				$this->success('请登录',url('weixin/index'),['id'=>1]);
			}	
    }
  	public function user()
    {
        return $this->fetch('user');
    }
}
